/*
 * @Description:
 * @Author: mxy
 * @Date: 2022-12-08 10:15:40
 * @LastEditors: mxy
 * @LastEditTime: 2022-12-09 21:57:05
 */
import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  { path: '/detail/:id', component: () => import('@/views/Detail') },
  { path: '/login', component: () => import('@/views/Login') },
  { path: '/register', component: () => import('@/views/Register') },
  {
    path: '/',
    component: () => import('@/views/Home'),
    redirect: '/article',
    children: [
      { path: 'article', component: () => import('@/views/Layout/Article') },
      { path: 'collect', component: () => import('@/views/Layout/Collect') },
      { path: 'like', component: () => import('@/views/Layout/Like') },
      { path: 'user', component: () => import('@/views/Layout/User') }
    ]
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  if (to.path === '/login' || to.path === '/register') {
    next()
  } else {
    if (localStorage.getItem('mj-token')) {
      next()
    } else {
      next('/login')
    }
  }
})
export default router
